import { AuthTypes,ConfigModal,authMatchEnum,AuthObjType} from '@@/hw-auth/modal.d';

const resultAuthBoolProvider=(resultAuth:authMatchEnum,type:AuthTypes)=>{
  if (type==="route"){
    return resultAuth===authMatchEnum.pass;
  }
  return  resultAuth!==authMatchEnum.notPass;
}//如果是 route类型只返回通过的，不是则返回不通过的

const authChecker=(auth:AuthObjType|string,config?:ConfigModal={})=>{
  const {checker,authKey="auth",type="component",typeKey="type",resultType="bool"}=config;
  if (!checker){
    return true;
  }
  if (typeof auth==="string"){
    return checker(auth,type);
  }
  const itemAuth=auth[authKey];
  const itemType=auth[typeKey]||type;
  const resultAuth=checker.check(itemAuth,itemType);
  if (resultType==="status"){
   return resultAuth;
  }
  return resultAuthBoolProvider(resultAuth,itemType);
}//单个验证

export const singleAuth=(auth:AuthObjType|string,config?:ConfigModal)=>{
      const {resultType="bool"}=config;
      let authObj=auth;
      if (typeof auth==="string"){
          authObj={
            auth
          }
      }
      const resultAuth=authChecker(auth,config);
       if (resultType==="bool"){
           return resultAuth;
       }
      return {...authObj,status:resultAuth};
}//返回类型
